package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.vo.WXHomeFloorVO;
import com.cskaoyan.bean.vo.WXHomeIndexVO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.WXHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WXHomeServiceImpl implements WXHomeService {
    @Autowired
    CskaoyanmallGoodsMapper goodsMapper;
    @Autowired
    CskaoyanmallCouponMapper couponMapper;
    @Autowired
    CskaoyanmallCategoryMapper categoryMapper;
    @Autowired
    CskaoyanmallAdMapper adMapper;
    @Autowired
    CskaoyanmallBrandMapper brandMapper;
    @Autowired
    CskaoyanmallTopicMapper topicMapper;
    @Autowired
    CskaoyanmallGrouponMapper grouponMapper;

    @Override
    public WXHomeIndexVO index() {
        WXHomeIndexVO indexVO = new WXHomeIndexVO( );

        CskaoyanmallGoodsExample goodsExample = new CskaoyanmallGoodsExample( );
        goodsExample.createCriteria( ).andIsNewEqualTo(true);
        List<CskaoyanmallGoods> newGoods = goodsMapper.selectByExample(goodsExample);
        indexVO.setNewGoodsList(newGoods.subList(0, 6));

        CskaoyanmallGoodsExample goodsExample1 = new CskaoyanmallGoodsExample( );
        goodsExample1.createCriteria( ).andIsHotEqualTo(true);
        List<CskaoyanmallGoods> hotGoods = goodsMapper.selectByExample(goodsExample1);
        indexVO.setHotGoodsList(hotGoods.subList(0, 6));

        CskaoyanmallAdExample adExample = new CskaoyanmallAdExample( );
        List<CskaoyanmallAd> ads = adMapper.selectByExample(adExample);
        indexVO.setBanner(ads);

        CskaoyanmallTopicExample topicExample = new CskaoyanmallTopicExample( );
        List<CskaoyanmallTopic> topicList = topicMapper.selectByExample(topicExample);
        indexVO.setTopicList(topicList.subList(0, 4));

        CskaoyanmallCouponExample couponExample = new CskaoyanmallCouponExample( );
        List<CskaoyanmallCoupon> couponList = couponMapper.selectByExample(couponExample);
        indexVO.setCouponList(couponList.subList(0, 3));

        CskaoyanmallGrouponExample grouponExample = new CskaoyanmallGrouponExample( );
        List<CskaoyanmallGroupon> grouponList = grouponMapper.selectByExample(grouponExample);
        indexVO.setGrouponList(grouponList);

        CskaoyanmallBrandExample brandExample = new CskaoyanmallBrandExample( );
        List<CskaoyanmallBrand> brandList = brandMapper.selectByExample(brandExample);
        indexVO.setBrandList(brandList.subList(0, 4));

        CskaoyanmallCategoryExample categoryExample = new CskaoyanmallCategoryExample( );
        categoryExample.createCriteria( ).andLevelEqualTo("L1");
        List<CskaoyanmallCategory> categoryList = categoryMapper.selectByExample(categoryExample);
        indexVO.setChannel(categoryList);

        List<WXHomeFloorVO> floorVOS = new ArrayList<>( );

        for (CskaoyanmallCategory category : categoryList) {
            WXHomeFloorVO floorVO = new WXHomeFloorVO( );
            Integer id = category.getId( );
            String name = category.getName( );
            floorVO.setId(id);
            floorVO.setName(name);
            CskaoyanmallCategoryExample categoryExample1 = new CskaoyanmallCategoryExample( );
            categoryExample1.createCriteria( ).andPidEqualTo(id);
            List<CskaoyanmallCategory> categoryListL2 = categoryMapper.selectByExample(categoryExample1);

            ArrayList<CskaoyanmallGoods> goodsList = new ArrayList<>( );

            for (CskaoyanmallCategory categoryL2 : categoryListL2) {
                Integer l2Id = categoryL2.getId( );
                CskaoyanmallGoodsExample goodsExample2 = new CskaoyanmallGoodsExample( );
                goodsExample2.createCriteria( ).andCategoryIdEqualTo(l2Id);
                List<CskaoyanmallGoods> goodList = goodsMapper.selectByExample(goodsExample2);
                goodsList.addAll(goodList);
            }
            if (goodsList .size( ) >= 4) {
                floorVO.setGoodsList(goodsList.subList(0, 4));
                floorVOS.add(floorVO);
            }
        }
        indexVO.setFloorGoodsList(floorVOS.subList(0, 4));
        return indexVO;
    }
}

